package com.annoncesautomobile.application;

import java.util.Map.Entry;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;

import org.acra.ReportField;
import org.acra.collector.CrashReportData;
import org.acra.sender.ReportSender;
import org.acra.sender.ReportSenderException;

import android.util.Log;

public class MailService implements ReportSender {
	
	
	private void sendEmail(String messageFormated) throws AddressException, MessagingException {
        String host = "smtp.gmail.com";
        String address = "bug.report.aa@gmail.com";
         
        String from = "bug.report.aa@gmail.com";
        String pass = "40kourien";
        
        
        String to="bug.report.aa@gmail.com";
         
        Multipart multiPart;
        String finalString="";
         
        Properties props = System.getProperties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", address);
        props.put("mail.smtp.password", pass);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");
        Log.i("Check", "done pops");

        Session session = Session.getDefaultInstance(props, null);
        DataHandler handler=new DataHandler(new ByteArrayDataSource(finalString.getBytes(),"text/plain" ));
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setDataHandler(handler);
        Log.i("Check", "done sessions");
         
        multiPart=new MimeMultipart();
 
        InternetAddress toAddress;
        toAddress = new InternetAddress(to);
        message.addRecipient(Message.RecipientType.TO, toAddress);
        Log.i("Check", "added recipient");
        message.setSubject("Send Auto-Mail");
        message.setContent(multiPart);
        message.setText(messageFormated);
         
         
         
        Log.i("check", "transport");
        Transport transport = session.getTransport("smtp");
        Log.i("check", "connecting");
        transport.connect(host,address , pass);
        Log.i("check", "wana send");
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
         
        Log.i("check", "sent");
 
    }

	@Override
	public void send(CrashReportData report) throws ReportSenderException {
		final StringBuilder sb = new StringBuilder();
		for(Entry<ReportField, String> aEntry: report.entrySet())
		{
			sb.append(aEntry.getKey().toString() + " - " + aEntry.getValue());
			sb.append("\n");
		}
		
		try {
			sendEmail(sb.toString());
		} catch (AddressException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (MessagingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		/*
		AsyncTask<Void, Void, String> asyncTaskSendMail = new AsyncTask<Void, Void, String>() {

			@Override
			protected String doInBackground(Void... params) {
				try {
					sendEmail(sb.toString());
				} catch (AddressException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					System.out.println("Erreur sendMail1");
				} catch (MessagingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					System.out.println("Erreur sendMail2");
				}
				return "1";
			}
			
		};
		
		try {
			asyncTaskSendMail.execute().get(2000,TimeUnit.MILLISECONDS);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
	}
}
